package com.wcj.test;

public class 困于环中的机器人 {


    public boolean isRobotBounded(String instructions) {
        char curDirect = 'N';
        int[] position ={0,0};
        for (int j = 0; j < 4; j++){
            for (int i = 0 ; i < instructions.length(); i++){
                char curCommond = instructions.charAt(i);
                // 前进改变坐标
                if (curCommond == 'G'){
                    position = changePosition(position,curDirect);
                }else { // 改变方向
                    curDirect = changeDirect(curCommond,curDirect);
                }
            }
            if (position[0] == 0 && position[1] == 0){
                return true;
            }
        }

        if (position[0] == 0 && position[1] == 0){
            return true;
        }else
            return false;
    }

    private char changeDirect(char curCommond, char curDirect) {
        if (curCommond == 'L'){
            switch (curDirect){
                case 'N': return 'W';
                case 'S': return 'E';
                case 'W': return 'S';
                case 'E': return 'N';
            }
        }else {
            switch (curDirect){
                case 'N': return 'E';
                case 'S': return 'W';
                case 'W': return 'N';
                case 'E': return 'S';
            }
        }
        return curDirect;
    }

    private int[] changePosition(int[] position, char curDirect) {
        switch (curDirect){
            case 'N': position[1] += 1;break;
            case 'S': position[1] -= 1;break;
            case 'W': position[0] -= 1;break;
            case 'E': position[0] += 1;break;
        }
        return position;
    }
}
